package com.base.mine.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.SystemUtil;
import com.base.commonlib.widget.CommonSingleDialog;
import com.base.mine.R;
import com.base.mine.bean.AccountPwdBean;
import com.base.mine.bean.Version;
import com.base.mine.config.RouteConfig;
import com.base.mine.view.PwdLoginView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/2/25.
 */

public class PwdLoginPresenter extends BasePresenter<PwdLoginView>{
    public PwdLoginPresenter(PwdLoginView view, Activity activity) {
        super(view, activity);
    }
    public void login(final String mAccount, final String mPassword){
        mView.showPb_mes("正在登录请稍后。。。");
        HashMap<String, Object> param = new HashMap<>();
        param.put("loginName",mAccount);
        param.put("password",mPassword);
        param.put("deviceType","ANDROID");
        param.put("verifyCode", "");
        param.put("rememberMe", 0);
        param.put("platform", 1);
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-auth/login").content(param)
                .build().enqueue(User.class,new CallBackImpl<User>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(User user) {
                        mView.hidePb_mes();
                        BaseApp.saveUser(user);
                        RxBus2.getInstance().post(user);
                        if (mView.isRemmber()){
                            SpUtil.setRemmberPwd(true);
                            SpUtil.setUserAccount(mAccount);
                            SpUtil.setUserPw(mPassword);
                            AccountPwdBean accountPwdBean=new AccountPwdBean();
                            accountPwdBean.setAccount(mAccount);
                            accountPwdBean.setPwd(mPassword);
                            Gson gson=new Gson();
                            String accountJson= gson.toJson(accountPwdBean);
                            Set<String> strings=new HashSet<>();
                            Set<String> stringsDefault=new HashSet<>();
                            strings = SpUtil.getStringSet(stringsDefault);
                            strings.add(accountJson);
                            SpUtil.setUserAccountList(strings);
                        }else {
                            SpUtil.setRemmberPwd(false);
                        }
//                        ARouter.getInstance().build("/mail/structure").navigation();
//                        getJifeng(user);
                        if (TextUtils.isEmpty(user.getTenantId())&&TextUtils.isEmpty(user.getOrgId())&&TextUtils.isEmpty(user.getDeptId())){
                            ARouter.getInstance().build("/mail/structure")
                                    .withString("token",user.getToken())
                                    .navigation();
                        }else {
                            SpUtil.setToken(user.getToken());
                            ARouter.getInstance().build("/study/home").withInt("num",2).navigation(mActivity, new NavCallback() {
                                @Override
                                public void onArrival(Postcard postcard) {
                                    mActivity.finish();
                                }
                            });
                        }

                    }
                });

        mCompositeDisposable.add(enqueue);
    }

    @Override
    public void start() {
        checkVersion();
    }


    private void checkVersion(){
        mView.showPb_mes("正在检查版本");
        HashMap<String, String> param = new HashMap<>();
        param.put("appVersion", SystemUtil.packageName(mActivity));
        param.put("versionCode", SystemUtil.getVersionCode(mActivity)+"");
        param.put("appName", mActivity.getString(R.string.app_name));
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-basic/app/check")
                .content(param)
                .build().enqueue(Version.class, new CallBackImpl<Version>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(Version version) {
                        mView.hidePb_mes();
                        if (version.isHaveNewVersion()){
                            CommonSingleDialog commonDialog = new CommonSingleDialog(mActivity, "您有新版本：\n"+version.getDescription());
                            commonDialog.setOnPosClick(new CommonSingleDialog.OnPosClick() {
                                @Override
                                public void onClick() {
                                    ARouter.getInstance().build(RouteConfig.ACTIVITY_VERSION).navigation(mActivity, new NavCallback() {
                                        @Override
                                        public void onArrival(Postcard postcard) {
                                            mActivity.finish();
                                        }
                                    });
                                }
                            });
                            commonDialog.setCancelable(false);
                            commonDialog.show();
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    /**
     * 积分
     */
    public void getJifeng(final User user){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("settingKey","08020003");
        map.put("funcMainId", "edu");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-function/integral/save").content(map)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                    }

                    @Override
                    public void onNext(String str) {
                        mView.hidePb();
                        mView.showMes(str);
                        if (TextUtils.isEmpty(user.getTenantId())&&TextUtils.isEmpty(user.getOrgId())&&TextUtils.isEmpty(user.getDeptId())){
                            ARouter.getInstance().build("/mail/structure").navigation();
                        }else {

                            ARouter.getInstance().build("/study/home").navigation(mActivity, new NavCallback() {
                                @Override
                                public void onArrival(Postcard postcard) {
                                    mActivity.finish();
                                }
                            });
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }
}
